<?php
/**
 * Created by jinqiang on 2024/7/15 16:29.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Wechat;


use App\Http\Controllers\Controller;
use App\Http\Services\Wechat\AntifakeService;
use Illuminate\Http\Request;

class AntifakeController extends Controller
{
    //获取微信scheme小程序跳转链接
    public function scheme(AntifakeService $antifakeService)
    {
        $res = $antifakeService->getScheme();
        return $res['code'] === 0 ? success("小程序跳转链接", $res['data']) : error("跳转小程序失败", $res);
    }

    //获取授权用户信息
    public function authlogin(Request $request, AntifakeService $antifakeService)
    {
        $this->validate($request, [
            'code' => 'required',
            'sub' => 'required|in:kz,js'
        ]);
        $param = $request->all();
        //info("用户授权信息", [$param]);
        $res = $antifakeService->search($param['sub'], $param['code']);
        return result($res, $res['msg']);
    }

    //添加授权用户信息
    public function addUser(Request $request, AntifakeService $antifakeService)
    {
        $this->validate($request, [
            'sub' => 'required',
            'nickname' => 'required',
            'avatarurl' => 'required',
            'openid' => 'required',
            'sex' => 'required',
        ]);
        $param = $request->all();
        $res = $antifakeService->addUser($param);
        return result($res, $res['msg']);
    }

    //获取用户手机号码
    public function getPhone(Request $request, AntifakeService $antifakeService)
    {
        $this->validate($request, [
            'code' => 'required',
        ]);
        $param = $request->all();
        $res = $antifakeService->getPhone($param['code']);
        return result($res, $res['msg']);
    }

    //扫码验证
    public function scancode(Request $request, AntifakeService $antifakeService)
    {
        $this->validate($request, [
            'number' => 'required',
            'deviceId' => 'required'
        ]);
        $param = $request->all();
        $res = $antifakeService->scancode($param['number'], $param['deviceId']);;
        return result($res, $res['msg']);
    }
}
